package com.my.blog.website.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import com.my.blog.website.constant.RedisConst;
import com.my.blog.website.constant.WebConst;
import com.my.blog.website.controller.BaseController;
import com.my.blog.website.dto.DingDto;
import com.my.blog.website.interceptor.BaseInterceptor;
import com.my.blog.website.service.IDingSevice;
import com.my.blog.website.utils.MarkdownUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;


/**
 * @author geng
 * @title: DingShowController
 * @projectName blog
 * @description: TODO
 * @date 2019/12/17 -- 15:31
 */
@Controller
public class DingShowController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(BaseInterceptor.class);


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IDingSevice dingSevice;

    @GetMapping("/dings/{id}")
    public String types(@PathVariable Integer id, Pageable pageable, Model model) {
        //数据库查询

        //打卡内容存储在redis中

        List<DingDto> list = null;
        String dingList = stringRedisTemplate.opsForValue().get(RedisConst.REDIS_DING);
        if (dingList == null || dingList.length() == 0) {
            list = dingSevice.findNameAndDing();
            dingList = JSON.toJSONString(list);
            stringRedisTemplate.opsForValue().set(RedisConst.REDIS_DING, dingList);
            log.info("查询数据库打卡");
        } else {
            list = JSONObject.parseArray(dingList, DingDto.class);
            log.info("从redis中获取");
        }
        int dingCnt = 0;
        //每个人的打卡数
        for (DingDto dingDto : list) {
            dingCnt += dingDto.getCount();
        }
        model.addAttribute("dingCnt", dingCnt);
        model.addAttribute("userList", list);
        int page = pageable.getPageNumber() == 0 ? 1 : pageable.getPageNumber();
        //  log.warn("当前为{}页"+ page);
        if (id == -1) {
            model.addAttribute("page", dingSevice.getDingsPage(null, page, WebConst.DEFAULT_PAGE_SIZE));
        } else {
            model.addAttribute("page", dingSevice.getDingsByUser(id, null, page, WebConst.DEFAULT_PAGE_SIZE));
        }
        model.addAttribute("activeTypeId", id);
        return "ding";
    }

    public DingDto getAndConvert(DingDto tmp) {
        DingDto b = new DingDto();
        BeanUtils.copyProperties(tmp, b);
        String content = b.getContent();
        b.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        return b;
    }
}
